//
//  base_thread.cpp
//  testpcap
//
//  Created by bugforever on 1/10/15.
//  Copyright (c) 2015 bugforever. All rights reserved.
//

#include "base_thread.h"
#include "assert.h"

#define VO_DEFAULT_STACKSIZE (128 * 1024)

base_thread::base_thread()
:m_thread_handle(0)
,m_is_thread_running(false)
{
    ;
}

base_thread::~base_thread()
{
    assert( !is_thread_running() );
}

int base_thread::start_thread()
{
    long thread_id = 0;
    
    stop_thread();
    
    return create_thread( &m_thread_handle , &thread_id , thread_function_internal , this , 0 ,VO_DEFAULT_STACKSIZE );
}

void base_thread::stop_thread()
{
    wait_thread_exit( m_thread_handle );
    m_thread_handle = 0;
}

unsigned int base_thread::thread_function_internal( void * ptr_param )
{
    base_thread * ptr_thread = (base_thread *)ptr_param;
    
    ptr_thread->m_is_thread_running = true;
    
    ptr_thread->thread_function();
    
    ptr_thread->m_is_thread_running = false;
    
    exit_thread();
    
    return 0;
}

bool base_thread::is_thread_running()
{
    return m_is_thread_running;
}
